<template>
	<view class="default-background">
		<view class="index-page">
				<view class="index-bar">
					<input class="bar-input" v-model="searchText" @change="searchChange" placeholder="搜点什么..." maxlength="20"/>
					<view class="bar-btn">
						<view class="search-btn" @click="search()">搜索</view>
					</view>
				</view>
				<view class="picker-bar">
					<picker @change="districtPickerChange" :value="districtIndex" :range="districtArray" range-key="name" class="picker-item">
						<view>{{ districtArray[districtIndex].name }}</view>
					</picker>
					<picker @change="roomPickerChange" :value="roomIndex" :range="roomArray" range-key="name" class="picker-item">
						<view>{{ roomArray[roomIndex].name }}</view>
					</picker>
					<picker @change="parlorPickerChange" :value="parlorIndex" :range="parlorArray" range-key="name" class="picker-item">
						<view>{{ parlorArray[parlorIndex].name }}</view>
					</picker>
					<picker @change="lavatoryPickerChange" :value="lavatoryIndex" :range="lavatoryArray" range-key="name" class="picker-item">
						<view>{{ lavatoryArray[lavatoryIndex].name }}</view>
					</picker>
					<picker @change="orderPickerChange" :value="orderIndex" :range="orderArray" range-key="name" class="picker-item">
						<view>{{ orderArray[orderIndex].name }}</view>
					</picker>
				</view>
				<scroll-view>
					<view class="house-item" v-for="house in this.houseList" @click="getDetail(house.id)">
						<view class="img-group">
							<image :src="house.firstImg" class="first-img" mode="aspectFill"></image>
						</view>
						<view class="text-group">
							<view class="title limit-one-line">{{house.title}}</view>
							<view class="price" v-if="house.type==0">{{house.price}}元/每月</view>
							<view class="price" v-if="house.type==1">{{moneyFormat(house.price)}}元</view>
							<view class="introduce limit-one-line">{{house.introduce}}</view>
							<!-- <uni-dateformat :date="house.createTime" format="yyyy-MM-dd hh:mm:ss" class="create-time"></uni-dateformat> -->
						</view>
					</view>
					<!-- <view class="page-bottom"></view> -->
				</scroll-view>
		</view>
	</view>
</template>

<script>
	import config  from "@/config/app.js"
	export default {
		data() {
			return {
				searchText: "",
				page: 1,
				limit: 6,
				houseList: [],
				districtArray: [{ name: '地区' }, { name: '城厢区' }, { name: '涵江区' }, { name: '荔城区' }, { name: '秀屿区' }, { name: '仙游县' }],
				districtIndex: 0,
				roomArray: [{ name: '房间' }, { name: '1房' }, { name: '2房' }, { name: '3房' }, { name: '4房' }, { name: '5房' }],
				roomIndex: 0,
				parlorArray: [{ name: '客厅' }, { name: '1厅' }, { name: '2厅' }, { name: '3厅' }, { name: '4厅' }, { name: '5厅' }],
				parlorIndex: 0,
				lavatoryArray: [{ name: '卫生间' }, { name: '1卫' }, { name: '2卫' }, { name: '3卫' }, { name: '4卫' }, { name: '5卫' }],
				lavatoryIndex: 0,
				orderArray: [{ name: '排序' }, { name: '时间降序' }, { name: '时间升序' }, { name: '价格降序' }, { name: '价格升序' }],
				orderIndex: 0,
			}
		},
		methods: {
			publishHouse() {
				uni.navigateTo({
					url: "./publishHouse"
				})
			},
			async getHouseList() {
				this.$request.request({
					url: "/house/list",
					method: "GET",
					data: {
						page: this.page,
						limit: this.limit,
						searchText: this.searchText,
						district: this.districtIndex > 0 ? this.districtArray[this.districtIndex].name : '',
						room: this.roomIndex > 0 ? this.roomIndex : '',
						parlor: this.parlorIndex > 0 ? this.parlorIndex : '',
						lavatory: this.lavatoryIndex > 0 ? this.lavatoryIndex : '',
						order: this.orderIndex > 0 ? this.orderArray[this.orderIndex].name : ''
					}
				}).then(res=>{
					if (res.code == 200) {
						for (let i=0; i<res.data.length; i++) {
							res.data[i].firstImg = config.IMG_URL + JSON.parse(res.data[i].pic)[0]
						}
						if (res.data.length <= 0) {
							uni.showToast({
								title: "没有更多数据了~",
								icon: "none"
							})
							return;
						} else {
							this.houseList = this.houseList.concat(res.data)
						}
					}
				})
			},
			search() {
				this.houseList = [];
				this.page = 1;
				this.getHouseList();
			},
			getDetail(id) {
				uni.navigateTo({
					url: "./detail?id=" + id
				})
			},
			districtPickerChange(e) {
				this.districtIndex = e.detail.value;
				this.houseList = [];
				this.page = 1;
				this.getHouseList();
			},
			roomPickerChange(e) {
				this.roomIndex = e.detail.value;
				this.houseList = [];
				this.page = 1;
				this.getHouseList();
			},
			parlorPickerChange(e) {
				this.parlorIndex = e.detail.value;
				this.houseList = [];
				this.page = 1;
				this.getHouseList();
			},
			lavatoryPickerChange(e) {
				this.lavatoryIndex = e.detail.value;
				this.houseList = [];
				this.page = 1;
				this.getHouseList();
			},
			orderPickerChange(e) {
				this.orderIndex = e.detail.value;
				this.houseList = [];
				this.page = 1;
				this.getHouseList();
			},
			moneyFormat(money) {
				if (money > 10000) {
					return parseFloat(money/10000).toFixed(2) + "万";
				} else {
					return money;
				}
			},
		},
		onLoad(options) {
			this.page = 1;
			this.houseList = [];
			this.searchText = options.searchText;
			this.getHouseList();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.houseList = [];
			this.searchText = "";
			this.getHouseList();
			uni.stopPullDownRefresh();
		},
		async onReachBottom() {
			this.page += 1;
			this.getHouseList();
		}
	}
</script>

<style lang="scss" scoped>
	
	.index-page {
		
		.index-bar {
			background-color: #11A8CD;
			display: flex;
			padding: 20rpx;
			
			.bar-input {
				flex: 1;
				background-color: #FFFFFF;
				border-radius: 50rpx;
				height: 60rpx;
				margin: 0 10rpx;
				padding: 0 30rpx;
			}
			
			.bar-btn {
				height: 60rpx;
				display: flex; 
				flex-direction: row; 
				align-items: center;
				justify-content: center;
				
				.search-btn {
					width: 100rpx;
					text-align: center;
					line-height: 60rpx;
					height: 60rpx;
					color: #FFFFFF;
					border-radius: 60rpx;
					
				}
			}
		}
		
		.picker-bar {
			display: flex;
			height: 80rpx;
			background-color: #FFFFFF;
			border-bottom: 3rpx solid #F2F1F6;
			align-items: center;
			
			.picker-item {
				flex: 1;
				text-align: center;
			}
			
		}
		
		.house-item {
			background-color: #FFFFFF;
			padding: 20rpx;
			display: flex;
			border-bottom: 3rpx solid #F2F1F6;
			
			.img-group {
				
				.first-img {
					width: 300rpx;
					height: 180rpx;
					border-radius: 20rpx;
				}
			}
			
			.text-group {
				padding: 0 20rpx;
				flex: 1;
				width: 360rpx;
				
				.title {
					font-size: 20px;
				}
				
				.price {
					margin-top: 30rpx;
					color: #F6416C;
				}
				
				.introduce {
					height: 40rpx;
					font-size: 16px;
					color: #999999;
				}
				
				.create-time {
					font-size: 16px;
					color: #999999;
				}
				
			}
		}
	}
	
</style>
